class MATRIX
-- This example show how use a matrix.

inherit
	IUP_INTERFACE
	IUP_GET_POINTER

create {ANY}
   make

feature {ANY}

	mt: IUP_MATRIX
	data: ARRAY2[STRING]
	bgcolors: ARRAY2[TUPLE[INTEGER, INTEGER, INTEGER]]

	make
		local
			gui: IUP
			i: STRING
			bg_button: IUP_BUTTON
			v1: IUP_VBOX
			w: IUP_DIALOG
		do
			gui := iup_open
			gui.load_images
			gui.load_iup_controls

			create bgcolors.make_filled([255, 255, 255], 25, 15)
			create data.make_filled("Hola", 25, 15)

			create mt.matrix
			mt.set_number_of_columns(15)
			mt.set_number_of_lines(25)
			mt.set_cb_background_color(agent background_color)
			mt.set_cb_value(agent matrix_value)
			mt.set_cb_value_edit(agent matrix_edited)
			mt.set_cb_drop(agent show_drop)
			mt.set_cb_menu_drop(agent show_menu_drop)
			mt.set_cb_drop_check(agent check_drop)
			mt.set_cb_drop_select(agent drop_select)

			mt.set_width_at_column(10, 0)
			mt.set_height_at_line(10, 0)

			create bg_button.button("Background color")
			bg_button.set_cb_action(agent click_button)

			create v1.vbox({ARRAY[IUP_WIDGET]} << bg_button, mt >>)

			create w.dialog(v1)
			w.set_title("Matrix test")
			w.set_predefined_size("HALF", "250")
			i := w.show

			gui.main_loop
			gui.close
		end

	click_button (b: IUP_BUTTON): STRING
		local
			tup: TUPLE[INTEGER, INTEGER, INTEGER, INTEGER]
			cell: TUPLE[INTEGER, INTEGER]
			gc: IUP_GET_COLOR
		do
			create gc.get_color_with_initial(0, 0, 0)

			tup := gc.launch_predefined_xy("IUP_CENTER", "IUP_CENTER")
			cell := mt.get_focus_cell

			if tup.integer_32_item(1).is_equal(1) then
				bgcolors.put([tup.integer_32_item(2), tup.integer_32_item(3), tup.integer_32_item(4)], cell.integer_32_item(1), cell.integer_32_item(2))
				mt.redraw
			end

			Result := "IUP_DEFAULT"
		end

	background_color (m: IUP_MATRIX; lin, col: INTEGER; r, g, b: POINTER): STRING
		do
			if attached bgcolors.item(lin, col) as t and
				lin >= 1 and
				col >= 1 then

				r.memory_copy(get_pointer(t.integer_32_item(1)), 8)
				g.memory_copy(get_pointer(t.integer_32_item(2)), 8)
				b.memory_copy(get_pointer(t.integer_32_item(3)), 8)

				Result := "IUP_DEFAULT"
			else
				Result := "IUP_IGNORE"
			end
		end

	matrix_value (m: IUP_MATRIX; lin, col: INTEGER): STRING
		do
			if col = 0 then
				Result := lin.out
			elseif lin = 0 then
				Result := col.out
			else
				Result := data.item(lin, col)
			end
		end

	matrix_edited (m: IUP_MATRIX; lin, col: INTEGER; new_value: STRING): STRING
		do
			data.put(new_value, lin, col)
			Result := "IUP_DEFAULT"
		end

	show_drop (m: IUP_MATRIX; d: IUP_DROP; lin, col: INTEGER): STRING
		do
			if lin.is_equal(5) and
				col.is_equal(3) then

				d.append_items({ARRAY[STRING]} << "January", "February", "March", "Aphril" >>)
				d.set_value(3)
				Result := "IUP_DEFAULT"
			else
				Result := "IUP_IGNORE"
			end
		end

	show_menu_drop (m: IUP_MATRIX; d: IUP_DROP; lin, col: INTEGER): STRING
		do
			if lin.is_equal(5) and
				col.is_equal(2) then

				d.append_items({ARRAY[STRING]} << "Cut", "Copy", "Paste" >>)
				d.set_image_at("IUP_EditCut", 1)
				d.set_image_at("IUP_EditCopy", 2)
				d.set_image_at("IUP_EditPaste", 3)
				d.set_value(2)

				Result := "IUP_DEFAULT"
			else
				Result := "IUP_IGNORE"
			end
		end

	check_drop (m: IUP_MATRIX; lin, col: INTEGER): STRING
		do
			if lin.is_equal(7) and
				col.is_equal(3) then
				Result := "IUP_CONTINUE"
			else
				Result := "IUP_IGNORE"
			end
		end

	drop_select (m: IUP_MATRIX; lin, col: INTEGER; d: IUP_DROP; t: STRING; i, v: INTEGER): STRING
		do
			io.put_string(t)
			io.put_new_line
			io.put_integer(i)
			io.put_new_line
			io.put_integer(v)
			io.put_new_line
			
			Result := "IUP_DEFAULT"
		end

end
